home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 1249 / fact.t < prev    next >
Text File  |  1997-04-18  |  610b  |  40 lines

  1. %
  2. % "fact.t" computes the factorial of a number 
  3. % using recursion
  4. %
  5. %   Sample program for the T Interpreter by:
  6. %
  7. %   Stephen R. Schmitt
  8. %   962 Depot Road
  9. %   Boxborough, MA 01719
  10. %
  11.  
  12. var number : int
  13.  
  14. program
  15.     
  16.     prompt "Enter an integer > 0:"
  17.     get number
  18.     put ""
  19.     put "N  = ", number
  20.     put "N! = ", factorial( number )
  21.  
  22. end program
  23.  
  24. function factorial( n : int ) : int
  25.     
  26.     assert n <= 12  % avoid numerical overflow  
  27.     
  28.     if n > 0 then
  29.  
  30.         n := n * factorial( n - 1 )
  31.  
  32.     else
  33.  
  34.         n := 1
  35.  
  36.     end if
  37.    
  38.     return n
  39.  
  40. end function